package com.hzmall.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.InputStreamReader;
import java.util.Properties;

@Slf4j
public class PropertiesUtil {
    private static Properties properties;
    static {
        String fileName = "hzmall.properties";
        properties = new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
        }catch (Exception e){
            log.error("读取配置文件发生异常",e);
        }
    }
    //默认获取配置文件中的属性值
    public static String getStringProperty(String key){
        String value = properties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return "null";
        }
        return value;
    }

    //获取配置文件中的属性值，如果没有，则使用传入的默认值
    public static String getStringPropertyWithDefault(String key, String defaultValue){
        String value = properties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return defaultValue;
        }
        return value;
    }

    public static Integer getIntegerPropertyWithDefault(String key, Integer defaultValue){
        String value = properties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    public static boolean getBooleanProperty(String key){
        String value = properties.getProperty(key);
        if (StringUtils.equals(value,"true")){
            return true;
        }
        return false;
    }

    public static Integer getIntegerProperty(String key){
        String value = properties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return -1;
        }
        return Integer.parseInt(value);
    }
}
